﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
namespace BridgeShips {
    public class Line : MonoBehaviour {

        private List<GameObject> arrowList = new List<GameObject>();
        private Transform[] game;

        private NavMeshAgent navPlayer;
        private NavMeshPath navPath;
        public GameObject prefab;//箭头

        public float tileSpacing = 20f;//箭头距离
        public static int num = 0;//位置记录

        void Start() {
            navPlayer = transform.GetComponent<NavMeshAgent>();
            navPath = new NavMeshPath();

            game = new Transform[3];
            game[0] = GameObject.Find( "location/game1" ).transform;
            game[1] = GameObject.Find( "location/game2" ).transform;
            game[2] = GameObject.Find( "location/game3" ).transform;
        }

        // Update is called once per frame
        void Update() {

            //if (num >= 0 && num <= 2)
            if (Input.GetMouseButtonDown( 0 )) {
                navPlayer.SetDestination( game[num].position );
                NavMesh.CalculatePath( transform.position, game[num].position, NavMesh.AllAreas, navPath );

                LineStart( navPath );
            }
        }

        private void LineStart( NavMeshPath navPath ) {

            List<GameObject> arrows = arrowList;
            StartCoroutine( ClearArrows( arrows ) );
            arrowList.Clear();

            if (navPath.corners.Length < 2) {
                return;
            }

            Quaternion planerot = Quaternion.identity;
            Debug.Log( navPath.corners.Length );
            for (int i = 0; i < navPath.corners.Length; i++) {
                float distance = 0;
                Vector3 offsetVector = Vector3.zero;
                if (i < navPath.corners.Length - 1) {
                    offsetVector = navPath.corners[i + 1] - navPath.corners[i];
                    if (i > 0) {
                        Vector3 offsetVectorPri = navPath.corners[i] - navPath.corners[i - 1];
                        float angle = Vector3.Angle( offsetVectorPri, offsetVector );
                        if (angle > 8.0f && angle < 25.0f) {
                            offsetVector = offsetVectorPri;
                        }
                    }
                    planerot = Quaternion.LookRotation( offsetVector );
                    distance = Vector3.Distance( navPath.corners[i + 1], navPath.corners[i] );
                    if (distance < tileSpacing)
                        continue;
                    planerot = Quaternion.Euler( 90, planerot.eulerAngles.y, planerot.eulerAngles.z );

                    float newSpacing = 0;
                    for (int j = 0; j < distance / tileSpacing; j++) {
                        newSpacing += tileSpacing;
                        var normalizedVector = offsetVector.normalized;
                        var position = navPath.corners[i] + newSpacing * normalizedVector;
                        GameObject go = Instantiate( prefab, position + Vector3.up, planerot );
                        arrowList.Add( go );
                    }
                }
                else {
                    GameObject go = Instantiate( prefab, navPath.corners[i] + Vector3.up, planerot );
                    arrowList.Add( go );
                }
            }
        }


        private IEnumerator ClearArrows( List<GameObject> arrows ) {
            if (arrowList.Count == 0)
                yield break;
            foreach (var item in arrows)
                Destroy( item );
        }
    }
}